package LeetCode.exercise;

import java.util.ArrayList;
import java.util.List;

public class LC46 {
    public static void main(String[] args) {
        int[] ints = {1,2,3};
        LC46 lc46 = new LC46();
        System.out.println(lc46.permute(ints));
    }
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] flag;
    public List<List<Integer>> permute(int[] nums) {
        flag = new boolean[nums.length];
        dfs(nums);
        return res;
    }
    public void dfs(int[] nums){
        if(path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < nums.length; i++){
            if(!flag[i]){
                path.add(nums[i]);
                flag[i] = true;
                dfs(nums);
                flag[i] = false;
                path.remove(path.size() -1);
            }
        }
    }
}
